﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common.Domain.Transactions;
using PostSharp.Aspects;

namespace Common.Application.Transactions
{
    [Serializable]
    public sealed class TransactionAttribute : OnMethodBoundaryAspect
    {
        private readonly ITransactionExecutor _transactionExecutor;

        public override void OnEntry(MethodExecutionArgs args)
        {
            _transactionExecutor.StartTransaction();
        }
        public override void OnExit(MethodExecutionArgs args)
        {
            _transactionExecutor.Dispose();
        }
        public override void OnException(MethodExecutionArgs args)
        {
            _transactionExecutor.Rollback();
        }
        public override void OnSuccess(MethodExecutionArgs args)
        {
            _transactionExecutor.Commit();
        }
    }
}
